package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatueCode;
import cn.tedu.egcar1.mapper.VehicleMapper;
import cn.tedu.egcar1.pojo.dto.VehicleAddParam;
import cn.tedu.egcar1.pojo.dto.VehicleListQuery;
import cn.tedu.egcar1.pojo.entity.Vehicle;
import cn.tedu.egcar1.pojo.vo.UserVO;
import cn.tedu.egcar1.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "02.车辆管理模块")
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam,@ApiIgnore HttpSession session){
        log.debug("开始处理【添加车辆】的请求，参数：{}", vehicleAddParam);

        // 检查用户是否登录
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatueCode.NOT_LOGIN, "用户未登录");
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult list(@ApiIgnore VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("开始处理【车辆列表】的请求，参数：{}", vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatueCode.NOT_LOGIN, "用户未登录");
        }
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        List<VehicleListVO> vehicleListVO= vehicleMapper.selectVehicle(brand,license);
        log.debug("车辆列表:{}",vehicleListVO);
        return JsonResult.ok(vehicleListVO);

    }
}
